package com.ht.module.bus.controller;

import com.ht.module.bus.dto.ScreenJsonDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.CollectionUtils;
import com.ht.module.bus.service.IBusScreenService;
import com.ht.util.*;
import com.ht.module.bus.entity.BusScreen;

import javax.validation.Valid;
import java.util.*;


/**
 * <p>
 * 大屏表 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2021-07-01
 */
@Api(tags = "大屏表 ")
@RestController
@RequestMapping("/bus-screen")
public class BusScreenController {


    @Autowired
    private IBusScreenService iBusScreenService;


    @ApiOperation(value = "分页查询所有大屏")
    @GetMapping("/findPage")
    public JsonResult findPage(Pager<BusScreen> pager, BusScreen busScreen) {
        return JsonResult.success(iBusScreenService.findPage(pager, busScreen));
    }


    @ApiOperation(value = "我的大屏查询")
    @GetMapping("/findPageByMe")
    @PreAuthorize("hasAnyAuthority('BUS_SCREEN_BY_MY')")
    public JsonResult findPageByMe(Pager<BusScreen> pager, BusScreen busScreen) {
        return JsonResult.success(iBusScreenService.findPageByMe(pager, busScreen));
    }

    @ApiOperation(value = "共享大屏查询")
    @GetMapping("/findPageByShare")
    public JsonResult findPageByShare(Pager<BusScreen> pager, BusScreen busScreen) {
        return JsonResult.success(iBusScreenService.findPageByShare(pager, busScreen));
    }



    /**
     * 通过id查询
     */
    @GetMapping("/getScreenJson/{id}")
    @ApiOperation(value = "获取大屏json数据")
    public JsonResult saveScreen(@PathVariable(value = "id") String id) {
        return JsonResult.success(iBusScreenService.getScreenJson(id));
    }


    /**
     * 通过id查询
     */
    @PostMapping("/saveScreenJson")
    @ApiOperation(value = "保存大屏json数据")
    public JsonResult saveScreenJson(@RequestBody @Valid ScreenJsonDto dto) {
        iBusScreenService.saveScreenJson(dto);
        return JsonResult.success();
    }



    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id/{id}")
    @ApiOperation(value = "通过id查询")
    public JsonResult getById(@PathVariable(value = "id") String id) {
        return JsonResult.success(iBusScreenService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public JsonResult add(@RequestBody @Valid BusScreen busScreen) {
        CommMethod.beanCreate(busScreen);
        iBusScreenService.save(busScreen);
        return JsonResult.success();
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/delete-by-id/{id}")
    @ApiOperation(value = "删除")
    public JsonResult delete(@PathVariable(value = "id") String id) {
        iBusScreenService.delById(id);
        return JsonResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public JsonResult updateById(@RequestBody @Valid BusScreen busScreen) {
        CommMethod.beanUpdate(busScreen);
        iBusScreenService.updateById(busScreen);
        return JsonResult.success();
    }

}
